/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> 
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation. You may not use, modify
or distribute this program under any other version of the
GNU Affero General Public License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.odinms.net.channel.handler;

import java.awt.Point;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MapleDisease;
import net.sf.odinms.client.status.MonsterStatus;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.server.life.MapleLifeFactory;
import net.sf.odinms.server.life.MapleMonster;
import net.sf.odinms.server.maps.MapleMapObject;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

public class MonsterCarnivalHandler extends AbstractMaplePacketHandler {

    @Override
    public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        int tab = slea.readByte();
        int num = slea.readByte();
        if (c.getPlayer().getAvailableCP() >= getPrice(num, tab)) {
            c.getPlayer().addAvailableCP(-getPrice(num, tab));
            if (tab == 0) { //only spawning for now..
                MapleMonster mob = MapleLifeFactory.getMonster(getMonsterIdByNum(num), c.getPlayer().getWorld());
                c.getPlayer().getMap().spawnMonsterOnGroundBelow(mob, randomizePosition(c.getPlayer().getMapId(), c.getPlayer().getCPQTeam()));
            } else if (tab == 1) {
                for (MapleMapObject players : c.getPlayer().getMap().getMapObjects()) {
                    if (players instanceof MapleCharacter) {
                        MapleCharacter player = (MapleCharacter) players;
                        player.addDisease(getDiseaseByNum(num));
                        break;
                    }
                }
            } else if (tab == 2) {
                for (MapleMapObject mob : c.getPlayer().getMap().getMapObjects()) {
                    if (mob instanceof MapleMonster) {
                        MapleMonster mobz = (MapleMonster) mob;
                        mobz.addMonsterBuff(getStatusByNum(num));
                        break;
                    }
                }
            }
        } else {
            c.getPlayer().dropMessage("You do not have enough CP");
            return;
        }
        c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.useCP(tab, num, c.getPlayer().getName()));
    }

    public Point randomizePosition(int mapid, int team) {
        int posx = 0;
        int posy = 0;
        if (mapid == 980000301) { //room 3 iirc
            posy = 162;
            if (team == 1){ //maple red goes left
                posx = rand(-1554, -151);
            } else { //maple blue goes right
                posx = rand(148, 1571);
            }
        }
        return new Point(posx, posy);
    }

    public int getMonsterIdByNum(int num) {
        int mid = 0;
        num++;
        switch (num) {
            case 1:
                mid = 9300127;
                break;
            case 2:
                mid = 9300128;
                break;
            case 3:
                mid = 9300129;
                break;
            case 4:
                mid = 9300130;
                break;
            case 5:
                mid = 9300131;
                break;
            case 6:
                mid = 9300132;
                break;
            case 7:
                mid = 9300133;
                break;
            case 8:
                mid = 9300134;
                break;
            case 9:
                mid = 9300135;
                break;
            case 10:
                mid = 9300136;
                break;
            default:
                mid = 210100; //LOL slime.. w/e, shouldn't happen
                break;
        }
        return mid;
    }

    public MonsterStatus getStatusByNum(int num) {
        num++;
        switch (num) {
            case 1:
                return MonsterStatus.WEAPON_ATTACK_UP;
            case 2:
                return MonsterStatus.WEAPON_DEFENSE_UP;
            case 3:
                return MonsterStatus.MAGIC_ATTACK_UP;
            case 4:
                return MonsterStatus.MAGIC_DEFENSE_UP;
            case 5:
                return MonsterStatus.ACC;
            case 6:
                return MonsterStatus.AVOID;
            case 7:
                return MonsterStatus.SPEED;
            case 8:
                return MonsterStatus.WEAPON_IMMUNITY;
            case 9:
                return MonsterStatus.MAGIC_IMMUNITY;
            default:
                return MonsterStatus.STUN;//shouldnt happen
        }
    }
    
    public MapleDisease getDiseaseByNum(int num) {
        num++;
        switch (num) {
            /*case 1:
                return MapleDisease.WEAPON_ATTACK_UP;
            case 2:
                return MapleDisease.WEAPON_DEFENSE_UP;
            case 3:
                return MapleDisease.MAGIC_ATTACK_UP;
            case 4:
                return MapleDisease.MAGIC_DEFENSE_UP;
            case 5:
                return MapleDisease.ACC;
            case 6:
                return MapleDisease.AVOID;
            case 7:
                return MapleDisease.SPEED;
            case 8:
                return MapleDisease.WEAPON_IMMUNITY;
            case 9:
                return MapleDisease.MAGIC_IMMUNITY;*/
            default:
                return MapleDisease.STUN;//shouldnt happen
        }
    }
    
    public int getPrice(int num, int tab) {
        int price = 0;
        num++;
        
        if (tab == 0) {
            switch (num) {
                case 1:
                case 2:
                    price = 7;
                    break;
                case 3:
                case 4:
                    price = 8;
                    break;
                case 5:
                case 6:
                    price = 9;
                    break;
                case 7:
                    price = 10;
                    break;
                case 8:
                    price = 11;
                    break;
                case 9:
                    price = 12;
                    break;
                case 10:
                    price = 30;
                    break;
                default:
                    price = 9999;//shouldnt happen
                    break;
            }
        } else if (tab == 1) {
            switch (num) {
                case 1:
                    price = 17;
                    break;
                case 2:
                case 4:
                    price = 19;
                    break;
                case 3:
                    price = 12;
                    break;
                case 5:
                    price = 16;
                    break;
                case 6:
                    price = 14;
                    break;
                case 7:
                    price = 22;
                    break;
                case 8:
                    price = 18;
                    break;
                default:
                    price = 9999;
                    break;
            }
        } else {
            switch (num) {
                case 1:
                case 3:
                    price = 17;
                    break;
                case 2:
                case 4:
                case 6:
                    price = 16;
                    break;
                case 5:
                    price = 13;
                    break;
                case 7:
                    price = 12;
                    break;
                case 8:
                case 9:
                    price = 35;
                    break;
                default:
                    price = 9999;//shouldnt happen
                    break;
            }
        }
        return price;
    }

    private static int rand(int lbound, int ubound) {
        return (int) ((Math.random() * (ubound - lbound + 1)) + lbound);
    }
}
